package com.tjsiqi.service;

import com.tjsiqi.Vo.*;
import com.tjsiqi.beans.PageQuery;
import com.tjsiqi.beans.PageResult;
import com.tjsiqi.dao.GybWeighRecordMapper;
import com.tjsiqi.dao.JblSendTruckOUTMapper;
import com.tjsiqi.dao.WebusercontrolMapper;
import com.tjsiqi.dto.SendCountDto;
import com.tjsiqi.dto.WeighCountDto;
import com.tjsiqi.dto.WeighCountDtoM;
import com.tjsiqi.exception.ParamException;
import com.tjsiqi.param.SendCountParam;
import com.tjsiqi.param.WeighCountParam;
import com.tjsiqi.param.WeighCountParamM;
import com.tjsiqi.util.DataHandleUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SysWeighCountService {
    @Autowired
    private GybWeighRecordMapper gybWeighRecordMapper;

    @Autowired
    private WebusercontrolMapper webusercontrolMapper;

    //按客户
    public PageResult<WeighProUnitVo> getWeighCountByProUnit(WeighCountParam weighCountParam, PageQuery page) {

        WeighCountDto weighCountDto = getWeighCDto(weighCountParam);

        int count = gybWeighRecordMapper.getWeighCountByProUnit(weighCountDto);

        if (count > 0){
            List<WeighProUnitVo> weighProUnitVoList = gybWeighRecordMapper.getPageListByProUnit(weighCountDto, page);
            return PageResult.<WeighProUnitVo>builder().total(count).data(weighProUnitVoList).build();
        }
        return PageResult.<WeighProUnitVo>builder().build();
    }

//  封装查询参数
    private static WeighCountDto getWeighCDto(WeighCountParam weighCountParam){
        WeighCountDto weighCountDto = new WeighCountDto();

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (StringUtils.isNotBlank(weighCountParam.getFromTime())) {
                weighCountDto.setFromTime(dateFormat.parse(weighCountParam.getFromTime()+" 00:00:00"));
            }else{
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR_OF_DAY, 0);
                // 分
                cal.set(Calendar.MINUTE, 0);
                // 秒
                cal.set(Calendar.SECOND, 0);
                // 毫秒
                cal.set(Calendar.MILLISECOND, 0);
//                cal.add(Calendar.DATE, -7);
                weighCountDto.setFromTime(cal.getTime());
            }
            if (StringUtils.isNotBlank(weighCountParam.getToTime())) {
                weighCountDto.setToTime(dateFormat.parse(weighCountParam.getToTime()+" 23:59:59"));
            }else{
                Calendar calTo = Calendar.getInstance();
                calTo.set(Calendar.HOUR_OF_DAY, 23);
                // 分
                calTo.set(Calendar.MINUTE, 59);
                // 秒
                calTo.set(Calendar.SECOND, 59);
                // 毫秒
                calTo.set(Calendar.MILLISECOND, 0);
                weighCountDto.setToTime(calTo.getTime());
            }
        } catch (Exception e) {
            throw new ParamException("传入的日期格式有问题,正确格式为：yyyy-MM-dd HH:mm:ss");
        }

        weighCountDto.setJbzCode(weighCountParam.getJbzCode());

        return weighCountDto;
    }
    //按车牌号
    public PageResult<WeighTruckVo> getWeighCountByTruck(WeighCountParam weighCountParam, PageQuery page) {

        WeighCountDto weighCountDto = getWeighCDto(weighCountParam);

        int count = gybWeighRecordMapper.getWeighCountByTruck(weighCountDto);

        if (count > 0){
            List<WeighTruckVo> weighTruckVoList = gybWeighRecordMapper.getPageListByTruck(weighCountDto, page);
            return PageResult.<WeighTruckVo>builder().total(count).data(weighTruckVoList).build();
        }
        return PageResult.<WeighTruckVo>builder().build();
    }
    //按存放位置
    public PageResult<WeighStorageVo> getWeighCountByStorage(WeighCountParam weighCountParam, PageQuery page) {

        WeighCountDto weighCountDto = getWeighCDto(weighCountParam);

        int count = gybWeighRecordMapper.getWeighCountByStorage(weighCountDto);

        if (count > 0){
            List<WeighStorageVo> weighStorageVoList = gybWeighRecordMapper.getPageListByStorage(weighCountDto, page);
            return PageResult.<WeighStorageVo>builder().total(count).data(weighStorageVoList).build();
        }
        return PageResult.<WeighStorageVo>builder().build();
    }
    //按货物名称
    public PageResult<WeighNameVo> getWeighCountByName(WeighCountParam weighCountParam, PageQuery page) {

        WeighCountDto weighCountDto = getWeighCDto(weighCountParam);

        int count = gybWeighRecordMapper.getWeighCountByName(weighCountDto);

        if (count > 0){
            List<WeighNameVo> weighNameVoList = gybWeighRecordMapper.getPageListByName(weighCountDto, page);
            return PageResult.<WeighNameVo>builder().total(count).data(weighNameVoList).build();
        }
        return PageResult.<WeighNameVo>builder().build();

    }

    //管理员查询生产厂家
    public PageResult<WeighProvideUnitVo> getWeighCountByProvideUnit(WeighCountParamM weighCountParamM, PageQuery page) {
        WeighCountDtoM weighCountDtoM = getWeighCDtoM(weighCountParamM);
        int count = gybWeighRecordMapper.getWeighCountByProvideUnit(weighCountDtoM);
        if (count > 0){
            List<WeighProvideUnitVo> weighProvideUnitVoList = gybWeighRecordMapper.getPageListByProvideUnit(weighCountDtoM, page);
            return PageResult.<WeighProvideUnitVo>builder().total(count).data(weighProvideUnitVoList).build();
        }
        return PageResult.<WeighProvideUnitVo>builder().build();
    }

    //管理员查询车牌号
    public PageResult<WeighTruckMVo> getWeighCountByWeightTruck(WeighCountParamM weighCountParamM, PageQuery page) {

        WeighCountDtoM weighCountDtoM = getWeighCDtoM(weighCountParamM);
        int count = gybWeighRecordMapper.getWeighCountByWeightTruck(weighCountDtoM);
        if (count > 0){
            List<WeighTruckMVo> weighTruckMVoList = gybWeighRecordMapper.getPageListByWeightTruck(weighCountDtoM, page);
            return PageResult.<WeighTruckMVo>builder().total(count).data(weighTruckMVoList).build();
        }
        return PageResult.<WeighTruckMVo>builder().build();
    }


    //初始化默认时间
    private static WeighCountDtoM getWeighCDtoM(WeighCountParamM weighCountParamM){
        WeighCountDtoM weighCountDtoM = new WeighCountDtoM();

        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if (StringUtils.isNotBlank(weighCountParamM.getFromTime())) {
                weighCountDtoM.setFromTime(dateFormat.parse(weighCountParamM.getFromTime()+" 00:00:00"));
            }else{
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.HOUR_OF_DAY, 0);
                // 分
                cal.set(Calendar.MINUTE, 0);
                // 秒
                cal.set(Calendar.SECOND, 0);
                // 毫秒
                cal.set(Calendar.MILLISECOND, 0);
//                cal.add(Calendar.DATE, -7);
                weighCountDtoM.setFromTime(cal.getTime());
            }
            if (StringUtils.isNotBlank(weighCountParamM.getToTime())) {
                weighCountDtoM.setToTime(dateFormat.parse(weighCountParamM.getToTime()+" 23:59:59"));
            }else{
                Calendar calTo = Calendar.getInstance();
                calTo.set(Calendar.HOUR_OF_DAY, 23);
                // 分
                calTo.set(Calendar.MINUTE, 59);
                // 秒
                calTo.set(Calendar.SECOND, 59);
                // 毫秒
                calTo.set(Calendar.MILLISECOND, 0);
                weighCountDtoM.setToTime(calTo.getTime());
            }
        } catch (Exception e) {
            throw new ParamException("传入的日期格式有问题,正确格式为：yyyy-MM-dd HH:mm:ss");
        }

//        System.out.println("获取特殊参数"+weighCountParamM.getSpecialParam());
        if (StringUtils.isNotBlank(weighCountParamM.getSpecialParam())) {
            weighCountDtoM.setSpecialParam("%" + weighCountParamM.getSpecialParam() + "%");
        }else{
            weighCountDtoM.setSpecialParam(null);
        }

        return weighCountDtoM;
    }


    //管理员查询搅拌站 材料名称
    public PageResult<WeighGoodsNameVo> getWeighCountByWeightGoodsName(WeighCountParamM weighCountParamM, PageQuery page) {

        WeighCountDtoM weighCountDtoM = getWeighCDtoM(weighCountParamM);
        int count = gybWeighRecordMapper.getWeighCountByWeightGoodsName(weighCountDtoM);

        if (count > 0){
            List<JbzInfoVo> jbzInfoVos = getjbzInfoM();
            List<WeighGoodsNameVo> weighGoodsNameVoList = gybWeighRecordMapper.getPageListByWeightGoodsName(weighCountDtoM, page);
//            List<jbzName> jbzNames =
            for (WeighGoodsNameVo weighGoodsNameVo:weighGoodsNameVoList) {
                for (JbzInfoVo jbzInfoVo: jbzInfoVos) {
                    if(jbzInfoVo.getCode().equals(weighGoodsNameVo.getUnitid()))
                    {
                        weighGoodsNameVo.setUnitid(jbzInfoVo.getMc());
                        continue;
                    }
                }
            }
            return PageResult.<WeighGoodsNameVo>builder().total(count).data(weighGoodsNameVoList).build();
        }
        return PageResult.<WeighGoodsNameVo>builder().build();
    }

    //管理员查询供货商
    public PageResult<WeighSendUnitVo> getWeighCountByWeightSendUnit(WeighCountParamM weighCountParamM, PageQuery page) {

        WeighCountDtoM weighCountDtoM = getWeighCDtoM(weighCountParamM);
        System.out.println("微信获取生产单位参数"+weighCountDtoM);
        List<JbzInfoVo> jbzInfoVos = getjbzInfoM();
        int count = gybWeighRecordMapper.getWeighCountByWeightSendUnit(weighCountDtoM);
        System.out.println("微信获取生产单位"+count);
        if (count > 0){
            List<WeighSendUnitVo> weighSendUnitVoList = gybWeighRecordMapper.getPageListByWeightSendUnit(weighCountDtoM, page);
            for (WeighSendUnitVo weighSendUnitVo:weighSendUnitVoList) {
                for (JbzInfoVo jbzInfoVo: jbzInfoVos) {
                    if(jbzInfoVo.getCode().equals(weighSendUnitVo.getUnitid()))
                    {
                        weighSendUnitVo.setUnitid(jbzInfoVo.getMc());
                        continue;
                    }
                }
                System.out.println("微信获取生产单位"+weighSendUnitVo);
            }
            return PageResult.<WeighSendUnitVo>builder().total(count).data(weighSendUnitVoList).build();
        }
        return PageResult.<WeighSendUnitVo>builder().build();
    }


    //获取搅拌站对应列表
    public List<JbzInfoVo> getjbzInfoM(){
        List<JbzInfoVo> jbzInfos = webusercontrolMapper.getjbzInfoM();
        return jbzInfos;
    }
}
